# -*- coding: utf-8 -*-
"""
@Time    : 2024/8/1 13:35 
@Author  : ZhangShenao 
@File    : 3.文件的上传与保存.py 
@Desc    : 文件的上传与保存

使用Flask的request.files,可以接收上传的文件并保存
"""

from flask import Flask, request

app = Flask(__name__)
app.config["DEBUG"] = True


@app.route("/upload_file", methods=["POST"])
def upload_file() -> str:
    """上传文件并保存"""

    # 使用request.files获取上传的文件
    # 类型为FileStorage
    pic = request.files.get("pic")
    if pic is None:
        return "文件未找到"

    print(f"pic: {pic}, name: {pic.filename}, type: {type(pic)}")

    # 通过FileStorage.save方法保存文件
    pic.save(f"./{pic.filename}")
    return "文件上传并保存成功"


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)
